import {Component, OnDestroy, OnInit} from '@angular/core';
import {NzMessageService} from "ng-zorro-antd/message";
import {NzModalService} from "ng-zorro-antd/modal";
import {CustomService} from "../../shared/services/custom.service";
import {ConfigurationService} from "../../shared/services/configuration.service";
import {ConfigService} from "../../shared/services/config.service";
import {Router} from "@angular/router";
import {ProductConfig} from "../../shared/model/product-config";

@Component({
    selector: 'app-setting',
    templateUrl: './setting.component.html',
    styleUrls: ['./setting.component.scss']
})
export class SettingComponent implements OnInit, OnDestroy {

    config: ProductConfig;
    collection: any;
    keywords: any;

    constructor(private customService: CustomService, private configService: ConfigService, private modal: NzModalService, private message: NzMessageService,
                private configurationService: ConfigurationService, public router: Router) {

    }

    ngOnInit() {
        this.initData();
    }

    ngOnDestroy(): void {
    }

    initData() {
        this.config = ProductConfig.getInstance();
        this.collection = {};
        this.keywords = {};
    }

    addItem(element, type){
        const data = this.config.getCollection(type);
        const display = this.getCollection(type);
        data.push(element);
        display.push(element);
        this.refreshCollection(type);
        this.configurationService.create({
            type: "Product",
            class: type,
            data: element.toJSON()
        }).subscribe( res => {
            this.message.create('success','添加成功！');
        })
    }

    editItem(element, type) {
        this.configurationService.update({
            data: element.toJSON()
        }).subscribe( res => {
            this.message.create('success','保存成功！');
        })
    }

    removeItem(element, type, index) {
        this.modal.confirm({
            nzTitle: '提示！',
            nzContent: '是否确认删除，删除后将不可恢复？',
            nzOkText: '确定',
            nzOkType: 'primary',
            nzOnOk: () => {
                const data = this.config.getCollection(type);
                const display = this.getCollection(type);

                const data_index = data.findIndex(item => item._id == element._id);
                data_index != -1 && data.splice(data_index, 1);

                const display_index = display.findIndex(item => item._id == element._id);
                display_index != -1 && display.splice(display_index, 1);

                this.refreshCollection(type);
                this.configurationService.delete({
                    data: element.toJSON()
                }).subscribe( res => {
                    this.message.create('success','删除成功！');
                })
            }
        });
    }

    getCollection(type) {
        if (!this.collection[type]) {
            const array = this.config.getCollection(type);
            this.collection[type] = [...array];
        }
        return this.collection[type];
    }

    refreshCollection(type) {
        this.collection[type] = [...this.collection[type]];
    }

    setCollection(type, array){
        this.collection[type] = array;
    }

    sort(sort: { key: string, value: string }, type){
        if (sort.key && sort.value) {
            const before = this.getCollection(type);
            before.sort((a, b) => (sort.value === 'ascend') ? (a[sort.key].toString().localeCompare(b[sort.key].toString())) : (b[sort.key].toString().localeCompare(a[sort.key].toString())));
            this.refreshCollection(type);
        }
    }

    search(type) {
        const source = this.config.getCollection(type);
        const array = source.filter(item => (item.name && item.name.indexOf(this.keywords[type]) !== -1) || (item.code && item.code.indexOf(this.keywords[type]) !== -1) );
        this.setCollection(type, array);
    }

}
